home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_091 / adlrun / rtparse.c < prev   
C/C++ Source or Header  |  1992-05-06  |  9KB  |  381 lines

  1. #include <stdio.h>
  2.  
  3. #include "adltypes.h"
  4. #include "adlprog.h"
  5. #include "adldef.h"
  6. #include "adlrun.h"
  7.  
  8. #define FIN  100    /* Final state */
  9. #define XXX -100    /* Default action in action table */
  10. #define ERR -100    /* Error state in transit table */
  11.  
  12. int16
  13.     state,        /* Current state for finite state machine */
  14.     Tnoun,        /* Temporary storage for current noun. */
  15.     Tmod,        /* Temporary storage for current adjective. */
  16.     P1, P2,        /* Temporary storage for prepositions */
  17.     C1;            /* Temporary storage for the last CONJ typed. */
  18. char
  19.     kludge[ SLEN ],    /* Static area for s */
  20.     s_str[ SLEN ];    /* End-of-string save for TELLER */
  21.  
  22.  
  23. int16    trantab[ 15 ][ 10 ] = {
  24. /*
  25.  *        Transit table for state machine
  26.  *
  27.  *                   NOUN
  28.  *      PREP VERB STR  ADJ  NOUN SYN  ART  CONJ ","  SEP
  29.  */
  30. /* 0 */ {   4,   1,   2,  12,  12,  12,  12, ERR, ERR, FIN },
  31. /* 1 */ {   4,   2,   2,   2,   2,   2,   2, ERR, ERR, FIN },
  32. /* 2 */ {   4,   3,   3,   3,   3,   3,   3,   5,   5, FIN },
  33. /* 3 */ {  11, ERR, ERR, ERR, ERR, ERR, ERR,   6,   6, FIN },
  34. /* 4 */ {   9,   7,   7,   7,   7,   7,   7, ERR, ERR, FIN },
  35. /* 5 */ { ERR,   8,   8,   8,   8,   8,   8,   5,   5, ERR },
  36. /* 6 */ { ERR,   3,   3,   3,   3,   3,   3,   6,   6, ERR },
  37. /* 7 */ {   9, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, FIN },
  38. /* 8 */ {   4, ERR, ERR, ERR, ERR, ERR, ERR,   5,   5, FIN },
  39. /* 9 */ { ERR,  10,  10,  10,  10,  10,  10, ERR, ERR, FIN },
  40. /*10 */ {  11, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, FIN },
  41. /*11 */ { ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, FIN },
  42. /*12 */ {   4,   3,   3,   3,   3,   3,   3,   5,  13, FIN },
  43. /*13 */ { ERR,  14,  14,   8,   8,   8,   8, ERR, ERR, ERR },
  44. /*14 */ { ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, FIN } };
  45.  
  46. int16    actions[ 15 ][ 10 ] = {
  47. /*
  48.  *        Action table for state machine
  49.  *                   NOUN
  50.  *      PREP VERB STR  ADJ  NOUN SYN  ART  CONJ ","  SEP
  51.  */
  52. /* 0 */ {   2,   0,   1,   1,   1,   1,   1, 500, 500, XXX },
  53. /* 1 */ {   3,   1,   1,   1,   1,   1,   1, 501, 501, XXX },
  54. /* 2 */ {   7,   5,   5,   5,   5,   5,   5,   6,   6,   4 },
  55. /* 3 */ {   2, 502, 502, 502, 502, 502, 502,   8,   8, XXX },
  56. /* 4 */ {   9,   1,   1,   1,   1,   1,   1, 501, 501,  10 },
  57. /* 5 */ { 501,  11,  11,  11,  11,  11,  11,   8,   8, 501 },
  58. /* 6 */ { 501,  11,  11,  11,  11,  11,  11,   8,   8, 501 },
  59. /* 7 */ {  13, 502, 502, 502, 502, 502, 502, 502, 502,  12 },
  60. /* 8 */ {   2, 502, 502, 502, 502, 502, 502,   8,   8, XXX },
  61. /* 9 */ { 501,  15,  15,  15,  15,  15,  15, 501, 501,  14 },
  62. /*10 */ {   2, 502, 502, 502, 502, 502, 502, 502, 502, XXX },
  63. /*11 */ { 503, 503, 503, 503, 503, 503, 503, 503, 503,  10 },
  64. /*12 */ {   7,   5,   5,   5,   5,   5,   5,   6,  16,   4 },
  65. /*13 */ { 501,  19,  18,  17,  17,  17,  17, 501, 501, 501 },
  66. /*14 */ { 503, 503, 503, 503, 503, 503, 503, 503, 503, XXX } };
  67.  
  68. static
  69. p_error( n )
  70. int
  71.     n;
  72. {
  73.     char
  74.     str[ 200 ];
  75.  
  76.     switch( n ) {
  77.     case -3  :
  78.         sprintf( str,
  79.             "I got confused trying to expand \"%s\" to \"%s\"\n",
  80.              s, xp );
  81.         break;
  82.     case -2  :
  83.         sprintf( str, "\"%s\" is not a valid abbreviation.\n", s );
  84.         break;
  85.     case -1  :
  86.         sprintf( str, "I don't know the word \"%s\".\n", s );
  87.         break;
  88.     case 500 :
  89.         sprintf( str, "\"%s\" is not a verb.\n", s );
  90.         break;
  91.     case 501 :
  92.         sprintf( str, "\"%s\" is not part of a noun phrase.\n", s );
  93.         break;
  94.     case 502 :
  95.         sprintf( str, "\"%s\" is not a preposition.\n", s );
  96.         break;
  97.     case 503 :
  98.         sprintf( str, "End of sentence expected.\n" );
  99.         break;
  100.     case 504 :
  101.         sprintf( str, "Illegal multiple word verb phrase.\n" );
  102.         break;
  103.     case 505 :
  104.         sprintf( str, "Too many direct objects.\n" );
  105.         break;
  106.     case 506 :
  107.         sprintf( str, "Illegal multiple word preposition.\n" );
  108.         break;
  109.     default  :
  110.         sprintf( str, "I don't understand that.\n", n );
  111.     }
  112.     sayer( str );
  113.     state = ERR;
  114. }
  115.  
  116.  
  117. static
  118. Get_Noun()
  119. {
  120.     Tmod = 0;
  121.     Tnoun = 0;
  122.  
  123.     /* Check to see whether the object is a string */
  124.     if( t_type == STRING ) {
  125.     Tnoun = t_val;
  126.     return 1;
  127.     }
  128.  
  129.     /* The object is not a string.  Find a noun phrase */
  130.  
  131.     /* Skip the article */
  132.     if( t_type == ART )
  133.     gettoken( 0 );
  134.  
  135.     /* Check to see whether we have a full-fledged object. */
  136.     if( t_type == NOUN_SYN ) {
  137.     Tnoun = objspace[ t_val ].noun;
  138.     Tmod = objspace[ t_val ].adj;
  139.     return 1;
  140.     }
  141.  
  142.     /* Check for a modifier (adjective or verb) */
  143.     if( t_type == ADJEC ) {
  144.     Tmod = t_val;
  145.     gettoken( 0 );
  146.     }
  147.     else if( t_type == VERB ) {
  148.     Tmod = -t_val;
  149.     gettoken( 0 );
  150.     }
  151.  
  152.     /* Check for the noun */
  153.     if( t_type == NOUN ) {
  154.     Tnoun = t_val;
  155.     return 1;
  156.     }
  157.  
  158.     if( t_type < 0 ) {
  159.     p_error( t_type );    /* Dictionary error */
  160.     return 0;
  161.     }
  162.     else if( (Tmod == 0) && (Tnoun == 0) ) {
  163.     p_error( 501 );        /* S is not a noun. */
  164.     return 0;
  165.     }
  166.     else {
  167.     read_t = 0;        /* Skip the next token */
  168.     return 1;        /* Found adjective, but no noun, */
  169.     }                /* so we'll leave it up to the dwimmer to figure
  170.                    it out */
  171. }
  172.  
  173.  
  174. Find_PP( Prep1, Mod, Noun, Prep2 )
  175. {
  176.     int16
  177.     i,
  178.     obj;
  179.  
  180.     if( Noun || Mod ) {
  181.     if( (obj = noun_exists( Mod, Noun )) < 0 )
  182.         return -1;
  183.     }
  184.     else
  185.     obj = 0;
  186.     for( i = 0; i < NUMPP; i++ )
  187.     if(    prepspace[ i ].first == Prep &&
  188.             prepspace[ i ].obj   == obj &&
  189.             prepspace[ i ].last  == t_val )
  190.     {
  191.         return prepspace[ i ].val;
  192.         }
  193.     return -1;
  194. }
  195.  
  196.  
  197. Find_VP( verb, prep )
  198. int16
  199.     verb, prep;
  200. {
  201.     int16
  202.     i;
  203.  
  204.     for( i = 0; i < NUMVS; i++ )
  205.     if( (verb == verbsyn[ i ].vrb) && (prep == verbsyn[ i ].prp) )
  206.         return verbsyn[ i ].val;
  207.     return -1;
  208. }
  209.  
  210.  
  211. Perform_Action( which )
  212. int
  213.     which;
  214. {
  215.     int16
  216.     x;            /* Temporary used for lookups */
  217.  
  218.     switch( which ) {
  219.     case  0 :
  220.         Verb = t_val;
  221.         break;
  222.     case  1 :
  223.         Get_Noun();
  224.         break;
  225.     case  2 :
  226.         P1 = t_val;
  227.         break;
  228.     case  3 :
  229.         if( (x = Find_VP( Verb, t_val )) >= 0 ) {
  230.         Verb = x;
  231.         state = 1;
  232.         }
  233.         else
  234.         P1 = t_val;
  235.         break;
  236.     case  4 :
  237.         Dmod[ 0 ] = Tmod;    Dnoun[ 0 ] = Tnoun;    Conj[ 0 ] = 0;
  238.         NumDobj = 1;
  239.         break;
  240.     case  5 :
  241.         Imod = Tmod;    Inoun = Tnoun;
  242.         Get_Noun();
  243.         Dmod[ 0 ] = Tmod;    Dnoun[ 0 ] = Tnoun;    Conj[ 0 ] = 0;
  244.         NumDobj = 1;
  245.         break;
  246.     case  6 :
  247.         Dmod[ 0 ] = Tmod;    Dnoun[ 0 ] = Tnoun;    Conj[ 1 ] = t_val;
  248.         NumDobj = 1;
  249.         break;
  250.     case  7 :
  251.         Dmod[ 0 ] = Tmod;    Dnoun[ 0 ] = Tnoun;    Conj[ 0 ] = 0;
  252.         NumDobj = 1;
  253.         P1 = t_val;
  254.         break;
  255.     case  8 :
  256.         Conj[ NumDobj ] = t_val;
  257.         break;
  258.     case  9 :
  259.         P2 = t_val;    Tmod = Tnoun = 0;
  260.         break;
  261.     case 10 :
  262.         if( (x = Find_VP( Verb, P1 )) >= 0 )
  263.         Verb = x;
  264.         else
  265.         p_error( 504 );        /* Illegal verb phrase */
  266.         break;
  267.     case 11 :
  268.         if( NumDobj >= NUMDO ) {
  269.         p_error( 505 );        /* Too many direct objects */
  270.         break;
  271.         }
  272.         Get_Noun();
  273.         Dmod[ NumDobj ] = Tmod;    Dnoun[ NumDobj ] = Tnoun;
  274.         NumDobj++;
  275.         break;
  276.     case 12 :
  277.         Imod = Tmod;        Inoun = Tnoun;
  278.         Prep = P1;
  279.         break;
  280.     case 13 :
  281.         P2 = t_val;
  282.         break;
  283.     case 14 :
  284.         if( (Tnoun != 0) || (Tmod != 0) ) {
  285.         Imod = Tmod;    Inoun = Tnoun;    Prep = P1;
  286.         if( (x = Find_VP( Verb, P2 )) >= 0 )
  287.             Verb = x;
  288.         else
  289.             p_error( 504 );    /* Illegal verb phrase */
  290.         }
  291.         else {
  292.         if( (x = Find_PP( P1, Tmod, Tnoun, P2 )) >= 0 ) {
  293.             P1 = x;
  294.             if( (x = Find_VP( Verb, P1 )) >= 0 )
  295.             Verb = x;
  296.             else
  297.             p_error( 504 );    /* Illegal verb phrase */
  298.         }
  299.         else
  300.             p_error( 506 );    /* Illegal prep phrase */
  301.         }
  302.         break;
  303.     case 15 :
  304.         if( (x = Find_PP( P1, Tmod, Tnoun, P2)) >= 0 )
  305.         Prep = x;
  306.         else {
  307.         p_error( 506 );        /* Illegal prep phrase */
  308.         break;
  309.         }
  310.         Get_Noun();
  311.         Imod = Tmod;    Inoun = Tnoun;
  312.         break;
  313.     case 16 :
  314.         C1 = t_val;
  315.         strcpy( s_str, PSTRING );
  316.         break;
  317.     case 17 :
  318.         Dmod[ 0 ] = Tmod;    Dnoun[ 0 ] = Tnoun;    Conj[ 0 ] = 0;
  319.         Conj[ 1 ] = C1;
  320.         Get_Noun();
  321.         Dmod[ 1 ] = Tmod;    Dnoun[ 1 ] = Tnoun;    NumDobj = 2;
  322.         break;
  323.     case 18 :
  324.         Imod = Tmod;    Inoun = Tnoun;
  325.         Dmod[ 0 ] = 0;    Dnoun[ 0 ] = t_val;    NumDobj = 1;
  326.         Verb = _TELLER;
  327.         break;
  328.     case 19 :
  329.         Imod = Tmod;    Inoun = Tnoun;
  330.         Dmod[ 0 ] = 0;    Dnoun[ 0 ] = newtstr( s_str );    NumDobj = 1;
  331.         Verb = _TELLER;
  332.         *PSTRING = '\0';
  333.         break;
  334.     }
  335. }
  336.  
  337.  
  338. initvars()
  339. {
  340.     int
  341.     i;
  342.  
  343.     read_t = 1;
  344.     state = 1;    /* Initial state */
  345.     s = kludge;
  346.     NumDobj = Prep = Iobj = Inoun = Imod = Verb = 0;
  347.     for( i=0; i < NUMDO; i++ )
  348.     Dobj[ i ] = Conj[ i ] = Dnoun[ i ] = Dmod[ i ] = 0;
  349. }
  350.  
  351.  
  352. parse()
  353. {
  354.     int16
  355.     done,
  356.     act;
  357.  
  358.     done = 0;    read_t = 1;    state = 0;
  359.     s = kludge;    Tmod = Tnoun = P1 = P2 = C1 = 0;
  360.     while( !done ) {
  361.     gettoken( 0 );
  362.     if( t_type < 0 )
  363.         p_error( t_type );        /* Dictionary error */
  364.     else {
  365.         act   = actions[ state ][ t_type - MIN_RT ];
  366.         state = trantab[ state ][ t_type - MIN_RT ];
  367.         if( state == ERR )
  368.         p_error( act );
  369.         else
  370.         Perform_Action( act );
  371.     }
  372.     if( state == FIN )
  373.         done = 2;
  374.     else if( state == ERR )
  375.         done = 1;
  376.     }    /* while */
  377.     return done - 1;
  378. }    /* parse */
  379.  
  380. /*** EOF rtparse.c ***/
  381.